package _2布局;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class CardLayoutFrame extends JFrame{

	public CardLayoutFrame() {
	
		
		JPanel p1 = new JPanel();
		JButton btn1 = new JButton("登录页");
		JButton btn2 = new JButton("注册页");
		p1.add(btn1);
		p1.add(btn2);
		
		JPanel card1 = new JPanel();
		card1.setBackground(Color.red);
		JLabel label1 = new JLabel("用户名:");
		JLabel label2 = new JLabel("密码:");
		card1.add(label1);
		card1.add(label2);
		JPanel card2 = new JPanel();
		card2.setBackground(Color.green);
		JLabel label3 = new JLabel("注册页面");
		card2.add(label3);
				
		this.add(p1,BorderLayout.NORTH);
		
		JPanel cards = new JPanel(new CardLayout());
		cards.add(card1,"card1");
		cards.add(card2,"card2");
		
		this.add(cards,BorderLayout.CENTER);
		
		
		btn1.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				CardLayout c = (CardLayout)cards.getLayout();
				c.show(cards, "card1");
				
			}
		});
		btn2.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {

				CardLayout c = (CardLayout)cards.getLayout();
				c.show(cards, "card2");
			}
		});
		
	}
}
